<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="ss" label-width="100px" >
				<uni-forms-item label="手术时间" name="sssj">
					<uni-datetime-picker type="date" :clear-icon="true" :disabled="isDisabled" v-model="ss.sssj" />
				</uni-forms-item>
				<uni-forms-item label="手术前治疗次数" name="ssqzlcs">
					<uni-easyinput :disabled="isDisabled" v-model="ss.ssqzlcs" type="number"  @change="autoSaven" placeholder="请输入手术前治疗次数" />
				</uni-forms-item>
				<uni-forms-item label="手术与否" name="sfss">
					<uni-data-checkbox :disabled="isDisabled" v-model="ss.sfss" @change="ycxsChange($event,'ssfsShow','ssfs')" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item label="手术方式" v-show="ssfsShow" name="ssfs">
					<uni-easyinput :disabled="isDisabled" v-model="ss.ssfs"  @change="autoSaven" placeholder="请输入手术方式" />
				</uni-forms-item>
				<uni-forms-item label="备注" name="remark">
					<uni-easyinput type="textarea" autoHeight :disabled="isDisabled" v-model="ss.remark"  @change="autoSaven" placeholder="请输入备注"/>
				</uni-forms-item>
				<button type="primary" :disabled="isDisabled" @click="submit">保存</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
import { updateSs,getDicts,sfkybjData } from "@/api/trms/trms"
import { getSs } from "@/api/trms/query"
export default {
	data() {
		return {
			patientId: null,
			isDisabled: false,
			userId: null,
			ssfsShow: false,
			// 表单数据
			ss: {},
			dictSf: [
				{
				  text: '是',
				  value: 1
				}, {
				  text: '否',
				  value: 0
				}
			],
			rules: {
				admissionNumber: {
				  rules: [{
					required: true,
					errorMessage: '住院号不能为空'
				  }]
				}
			}
		}
	},
	onLoad: function(option) {
		this.patientId = option.patientId;
		this.userId = option.userId;
		console.log("进入基本信息页面后、参数：",this.patientId)
		sfkybjData(this);
		if(this.patientId){
			// 查询详情
			this.getDataByPatientId();
		}
	},
	onReady() {
		this.$refs.form.setRules(this.rules)
	},
	methods: {
		/**
		 * 获取数据
		 */
		getDataByPatientId(){
			getSs(this.patientId).then(response => {
			  this.ss = response.data;
			  this.showAndHide(response.data);
			});
		},
		/**
		 * 修改
		 */
		submit(){
			this.$refs.form.validate().then(res => {
				updateSs(this.ss).then(response => {
				  if(response.code == 200){
					  this.$modal.msgSuccess("修改成功");
					  this.backWork();
					  return;
				  }
				  this.$modal.msgError(response.msg);
				});
			})
		},
		autoSaven(){
			this.$refs.form.validate().then(res => {
				updateSs(this.ss).then(response => {
				  console.log("修改成功");
				});
			})
		},
		/**
		 * 回到工作页面
		 */
		backWork(){
			setTimeout(function (){
				uni.navigateBack({
					delta: 1
				});
			},500);
		},
		ycxsChange(e,show,valId){
			const val = e.detail.value;
			this[show] = val == 1;
			if(Number(val) == 0 ){
				// 清理值
				this.ss[valId] = null;
			}
		},
		/**
		 * 隐藏或者显示
		 * @param {Object} jbxx 
		 */
		showAndHide(jbxx){
			this.ssfsShow = jbxx.sfss == 1;
		}
	}
}
</script>


<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .example {
    padding: 15px;
    background-color: #fff;
  }

  .segmented-control {
    margin-bottom: 15px;
  }

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }

  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }

  .button {
    display: flex;
    align-items: center;
    height: 35px;
    line-height: 35px;
    margin-left: 10px;
  }
</style>
